-- 新手副本特殊逻辑 --
local Obj = require("core.Obj")
local ObjMonster = require("core.ObjMonster")
local ObjHuman = require("core.ObjHuman")
local Msg = require("core.Msg")
local SceneEvent = require("core.SceneEvent")
local CopyHandler = require("copy.HandlerL")
local Buffer = require("buffer.Buffer")
local TaskAction =require("task.TaskAction")
local RoleUIControl = require("role.RoleUIControl")
local Copy = require("copy.CopyL")
local ShenbingLogic = require("shenbing.ShenbingLogic")
local HandlerL = require("scene.HandlerL")


NO_EXIT_COPY_TABLE = NO_EXIT_COPY_TABLE or {} -- 新手流程特殊逻辑 在这几个副本中不显示退出按钮
HAS_FOLLOW_NPC_COPY_TABLE = HAS_FOLLOW_NPC_COPY_TABLE or {421} -- 新手流程特殊逻辑 在这几个副本中显示跟随npc

----------------------------------------------------
-- 第一个副本hardcode定义 being --------------------
BEGIN_STORY_BORN_ID = 10	-- 打第一波小怪	
BEGIN_STORY_BORN_2_ID = 20  -- 打第二波小怪 
BEGIN_STORY_BORN_3_ID = 30  -- 第一次打张宝 
BEGIN_STORY_BORN_4_ID = 40  -- 采集赵云的枪 
BEGIN_STORY_BORN_5_ID = 50  -- 打张宝营地的小怪    
BEGIN_STORY_BORN_6_ID = 60  -- 打张宝 
BEGIN_STORY_BORN_7_ID = 70  -- 和赵云对话
BEGIN_STORY_BORN_8_ID = 80  -- 去到传送点 
   
BEGIN_STORY_BORN_SPEED_DOWN_BUFF_ID = 79 -- 新手一直到开铁骑系统前速度降低的buff  
BEGIN_STORY_BORN_SKILL_BUFF_ID = 82 -- 新手出生加技能的buff  
BEGIN_STORY_BORN_ZHAOYUN_ATK_BUFF_ID = 43 -- 化身赵云觉醒时加攻击力buff
BEGIN_STORY_BORN_ZHAOYUN_HP_BUFF_ID = 45 -- 化身赵云觉醒时回血buff
BEGIN_STORY_BORN_ZHAOYUN_SPEED_BUFF_ID = 80 -- 化身赵云觉醒时加速度buff
BEGIN_STORY_BORN_ZHAOYUN_HUIXIN_BUFF_ID = 83 -- 化身赵云觉醒时加会心状态buff
BEGIN_STORY_BORN_ZHAOYUN_ZUOYUE_BUFF_ID = 84 -- 化身赵云觉醒时加卓越状态buff
BEGIN_STORY_BORN_ZHAOYUN_BAOJI_BUFF_ID = 85 -- 化身赵云觉醒时加暴击状态buff
BEGIN_STORY_BORN_ZHAOYUN_AWAKE_BUFF_ID = 81 -- 化身赵云觉醒状态buff
 
 
BEGIN_STORY_MONSTER_ZHAOYUN_ID = 9999931 --赵云友方怪id

BEGIN_STORY_COPY_NEW_BORN  = 400		-- 长坂坡
-- 第一个副本hardcode定义 end --------------------
----------------------------------------------------

------------------------------------------------------------
-- 豫州营地大地图流程hardcode定义 being --------------------
OPEN_HORSE_TASK_ID = 110 -- 开放铁骑系统任务ID
CHOOSE_WEAPON_TASK_ID = 155 --选神兵任务ID
BEGIN_STORY_YUZHOU_ENTER_ID = 160 -- 和刘备对话进入豫州营地副本   
-- 豫州营地大地图流程hardcode定义 end ----------------------
------------------------------------------------------------


------------------------------------------------------------
-- 豫州营地副本流程hardcode定义 being ----------------------
BEGIN_STORY_YUZHOU_COPY_170_ID = 170		-- 打10个小怪 打完张飞出现
BEGIN_STORY_YUZHOU_COPY_180_ID = 180		-- 打15个小怪 打完张飞变npc
BEGIN_STORY_YUZHOU_COPY_190_ID = 190		-- 和张飞对话 
BEGIN_STORY_YUZHOU_COPY_195_ID = 195		-- 和张飞对话 完成后张飞npc变回友方怪
BEGIN_STORY_YUZHOU_COPY_200_ID = 200		-- 走到特定区域
BEGIN_STORY_YUZHOU_COPY_210_ID = 210		-- 打李催和手下
BEGIN_STORY_YUZHOU_COPY_220_ID = 220		-- 打幻影董卓 打完张飞变npc2
BEGIN_STORY_YUZHOU_COPY_230_ID = 230		-- 和张飞对话 完成后刷出传送阵

BEGIN_STORY_YUZHOU_MONSTER_ZHANGFEI_ID = 1999931 --张飞友方怪id


BEGIN_STORY_COPY_NEW_YUZHOU  = 402	-- 豫州营地副本id
-- 豫州营地副本流程hardcode定义 end -----------------------
------------------------------------------------------------

------------------------------------------------------------
-- 虎牢关流程hardecode定义 begin ---------------------------
BEGIN_STORY_COPY_NEW_HULAOGUAN  = 412	-- 虎牢关副本id
BEGIN_STORY_HULAOGUAN_280_ID = 280	-- 和大将军对话 对话完去虎牢关野外地图
BEGIN_STORY_HULAOGUAN_450_ID = 450	-- 和袁绍对话 完成后进虎牢关副本
BEGIN_STORY_HULAOGUAN_460_ID = 460	-- 和袁绍对话 完成后进洛阳
-- 虎牢关流程hardecode定义 end -----------------------------
------------------------------------------------------------

------------------------------------------------------------
-- 火烧洛阳流程hardecode定义 begin ---------------------------
BEGIN_STORY_COPY_NEW_LUOYANG = 407	-- 洛阳找玉玺副本
BEGIN_STORY_LUOYANG_550_ID = 550 -- 和女鬼对话进入找玉玺副本

BEGIN_STORY_LUOYANG_MAN_BUFF_1_ID = 30154	-- 找玉玺副本进入加buff男
BEGIN_STORY_LUOYANG_MAN_BUFF_2_ID = 30155	-- 找玉玺副本进入加buff男
BEGIN_STORY_LUOYANG_WOMAN_BUFF_1_ID = 30156 -- 找玉玺副本进入加buff女
BEGIN_STORY_LUOYANG_WOMAN_BUFF_2_ID = 30157 -- 找玉玺副本进入加buff女
-- 火烧洛阳流程hardecode定义 end -----------------------------
------------------------------------------------------------

--------------------------------------------------------------
-- 官渡之战流程hardecode定义 begin ---------------------------
BEGIN_STORY_GUANDU_690_ID = 690 -- 和于禁对话进入打淳于琼副本
BEGIN_STORY_COPY_GUANDU  = 421	-- 打淳于琼副本id
-- 官渡之战流程hardecode定义 end -----------------------------
--------------------------------------------------------------



function checkLogin(human)
	local finishTaskID = RoleUIControl.getMaxFinishMainTaskID(human)
	if finishTaskID < OPEN_HORSE_TASK_ID then -- 铁骑系统开放前 登录加一个减速buff
		Buffer.add(BEGIN_STORY_BORN_SPEED_DOWN_BUFF_ID, human, human)
	end
	
	if finishTaskID < BEGIN_STORY_BORN_8_ID then -- 做到这个任务前都应该在新手第一个副本中
		Buffer.add(BEGIN_STORY_BORN_SKILL_BUFF_ID, human, human)
		CopyHandler.copyEnter(human, BEGIN_STORY_COPY_NEW_BORN)
		return true
	end
	
	if human.db.shenbingLen == 0 then -- 选神兵任务如果正在做 登录时要弹出选择神兵界面
	   local id = RoleUIControl.getMaxDoingMainTaskID(human)
	   if id == CHOOSE_WEAPON_TASK_ID then
	       ShenbingLogic.notifyChooseShenbing(human)
	   end
	end
	
	if finishTaskID >= BEGIN_STORY_YUZHOU_ENTER_ID and finishTaskID < BEGIN_STORY_YUZHOU_COPY_230_ID then -- 这段任务之间应该无论如何进豫州副本
		CopyHandler.copyEnter(human, BEGIN_STORY_COPY_NEW_YUZHOU)
		return true
	end
end

local function changeAwake(human)
    ObjHuman.addAwake(human, 1000)
    HandlerL.CG_AWAKE(human)
end

local function unchangeAwake(human)
	Buffer.removeID(BEGIN_STORY_BORN_ZHAOYUN_ATK_BUFF_ID, human)
	Buffer.removeID(BEGIN_STORY_BORN_ZHAOYUN_HP_BUFF_ID, human)
	Buffer.removeID(BEGIN_STORY_BORN_ZHAOYUN_SPEED_BUFF_ID, human)
	Buffer.removeID(BEGIN_STORY_BORN_ZHAOYUN_HUIXIN_BUFF_ID, human)
	Buffer.removeID(BEGIN_STORY_BORN_ZHAOYUN_ZUOYUE_BUFF_ID, human)
	Buffer.removeID(BEGIN_STORY_BORN_ZHAOYUN_BAOJI_BUFF_ID, human)
	Buffer.removeID(BEGIN_STORY_BORN_ZHAOYUN_AWAKE_BUFF_ID, human, true)
end

local function addFriendMonster(human, monsterID)
	local monster = ObjMonster.create(monsterID)
    ObjMonster.enterScene(monster, human.scene_id,human.x - 100, human.y + 100)
end

local function removeFriendMonster(human, monsterID)
	-- 删除赵云这个友方怪
	local scene_id = human.scene_id
	for _, o in pairs(Obj.scenes[scene_id]) do
		if o.obj_type == Obj.TYPE_MONSTER and o.monster_id == monsterID then
			ObjMonster.destroy(o)
			break
		end
	end
end

local function luolei(human, action)
	local sendMsg = Msg.gc.GC_BEGIN_STORY_ACTION  
	sendMsg.action = action
	Msg.send(sendMsg, human.fd) 
end

-- CG_ENTER_SCENE_OK中才来跳转
function checkEnterSceneOk(human)
	if Copy.isCopyBySceneID(human.scene_id) == BEGIN_STORY_COPY_NEW_BORN then
		local maxDoing = RoleUIControl.getMaxDoingMainTaskID(human)
		if maxDoing == BEGIN_STORY_BORN_ID then
			-- 任务1 打第一批小怪
			TaskAction.playMovie(human, {"1001-1", 0}) -- 开场动画
			Obj.jump(human, 1522, 5095, Msg.gc.GC_JUMP)
			addFriendMonster(human, BEGIN_STORY_MONSTER_ZHAOYUN_ID)
			SceneEvent.doSceneEvent(human.scene_id, 1) 
  			SceneEvent.doSceneEvent(human.scene_id, 101)
		elseif maxDoing == BEGIN_STORY_BORN_2_ID then
			-- 任务2 打第二批小怪
			Obj.jump(human, 2900, 5000, Msg.gc.GC_JUMP)
			addFriendMonster(human, BEGIN_STORY_MONSTER_ZHAOYUN_ID)
			SceneEvent.doSceneEvent(human.scene_id, 3)
  			SceneEvent.doSceneEvent(human.scene_id, 101)
		elseif maxDoing == BEGIN_STORY_BORN_3_ID then
			-- 任务3 第一次打张宝
			Obj.jump(human, 1500, 3200, Msg.gc.GC_JUMP)
			addFriendMonster(human, BEGIN_STORY_MONSTER_ZHAOYUN_ID)
			SceneEvent.doSceneEvent(human.scene_id, 5)
  			SceneEvent.doSceneEvent(human.scene_id, 101)
  			luolei(human, 1)
		elseif maxDoing == BEGIN_STORY_BORN_4_ID then
			-- 任务4 采集赵云的枪
			Obj.jump(human, 1356, 2598, Msg.gc.GC_JUMP)
			SceneEvent.doSceneEvent(human.scene_id, 6)
  	  		SceneEvent.doSceneEvent(human.scene_id, 11)
  			SceneEvent.doSceneEvent(human.scene_id, 101)
  			luolei(human, 1)
		elseif maxDoing == BEGIN_STORY_BORN_5_ID then
			-- 任务5 变无双后打一批小怪
			Obj.jump(human, 2693, 1853, Msg.gc.GC_JUMP)
  			SceneEvent.doSceneEvent(human.scene_id, 8)
  			SceneEvent.doSceneEvent(human.scene_id, 9)
  	  		SceneEvent.doSceneEvent(human.scene_id, 11)
  			SceneEvent.doSceneEvent(human.scene_id, 101)
  			changeAwake(human)
  			luolei(human, 1)
		elseif maxDoing == BEGIN_STORY_BORN_6_ID then
			-- 任务6 第二次打张宝
			Obj.jump(human, 4300, 808, Msg.gc.GC_JUMP)
			SceneEvent.doSceneEvent(human.scene_id, 10)
  	  		SceneEvent.doSceneEvent(human.scene_id, 11)
  			SceneEvent.doSceneEvent(human.scene_id, 101)
  			changeAwake(human)
  			luolei(human, 1)
		elseif maxDoing == BEGIN_STORY_BORN_7_ID then
			-- 任务7 和赵云对话
			Obj.jump(human, 1485, 2555, Msg.gc.GC_JUMP)
  	  		SceneEvent.doSceneEvent(human.scene_id, 11)
  			SceneEvent.doSceneEvent(human.scene_id, 101)
  			changeAwake(human)
		elseif maxDoing == BEGIN_STORY_BORN_8_ID then
			-- 任务8 去到特定区域
			Obj.jump(human, 1485, 2555, Msg.gc.GC_JUMP)
  	  		SceneEvent.doSceneEvent(human.scene_id, 11)
		end
	end
	
	if Copy.isCopyBySceneID(human.scene_id) == BEGIN_STORY_COPY_NEW_YUZHOU then
		local maxDoing = RoleUIControl.getMaxDoingMainTaskID(human)

		if maxDoing == BEGIN_STORY_YUZHOU_COPY_170_ID then
			--打10个小怪 打完张飞出现
  	  		SceneEvent.doSceneEvent(human.scene_id, 1)
  	  		SceneEvent.doSceneEvent(human.scene_id, 2)
  	  	elseif maxDoing == BEGIN_STORY_YUZHOU_COPY_180_ID then
  	  		-- 打15个小怪 打完张飞变npc
			Obj.jump(human, 8700, 3352, Msg.gc.GC_JUMP)
  	  	  	SceneEvent.doSceneEvent(human.scene_id, 4)
  	  		SceneEvent.doSceneEvent(human.scene_id, 5)
  	  		SceneEvent.doSceneEvent(human.scene_id, 100)
  			--addFriendMonster(human, BEGIN_STORY_YUZHOU_MONSTER_ZHANGFEI_ID)
		elseif maxDoing == BEGIN_STORY_YUZHOU_COPY_190_ID then
			--和张飞对话
			Obj.jump(human, 7223, 3639, Msg.gc.GC_JUMP)
  	  	  	SceneEvent.doSceneEvent(human.scene_id, 5)
  	  	  	SceneEvent.doSceneEvent(human.scene_id, 6)
		elseif maxDoing == BEGIN_STORY_YUZHOU_COPY_195_ID then
			-- 和张飞对话 完成后张飞npc变回友方怪
			Obj.jump(human, 7223, 3639, Msg.gc.GC_JUMP)
			SceneEvent.doSceneEvent(human.scene_id, 5)
  	  	  	SceneEvent.doSceneEvent(human.scene_id, 6)
  	  	elseif maxDoing == BEGIN_STORY_YUZHOU_COPY_200_ID then
  	  		-- 走到特定区域
			Obj.jump(human, 7223, 3639, Msg.gc.GC_JUMP)
  			--addFriendMonster(human, BEGIN_STORY_YUZHOU_MONSTER_ZHANGFEI_ID)
  	  		SceneEvent.doSceneEvent(human.scene_id, 101)
  			
  	  	elseif maxDoing == BEGIN_STORY_YUZHOU_COPY_210_ID then
  	  		-- 打李催和手下
			Obj.jump(human, 5345, 3262, Msg.gc.GC_JUMP)
  	  	  	SceneEvent.doSceneEvent(human.scene_id, 9)
  			--addFriendMonster(human, BEGIN_STORY_YUZHOU_MONSTER_ZHANGFEI_ID)
  	  		SceneEvent.doSceneEvent(human.scene_id, 101)
  			
  	  	elseif maxDoing == BEGIN_STORY_YUZHOU_COPY_220_ID then
  	  		-- 打幻影董卓 打完张飞变npc2
			Obj.jump(human, 2121, 1417, Msg.gc.GC_JUMP)
  	  	  	SceneEvent.doSceneEvent(human.scene_id, 10)
  			--addFriendMonster(human, BEGIN_STORY_YUZHOU_MONSTER_ZHANGFEI_ID)
  	  		SceneEvent.doSceneEvent(human.scene_id, 101)
  			
  	  	elseif maxDoing == BEGIN_STORY_YUZHOU_COPY_230_ID then
  	  		-- 和张飞对话 完成后刷出传送阵
			Obj.jump(human, 2121, 1417, Msg.gc.GC_JUMP)
  	  	  	SceneEvent.doSceneEvent(human.scene_id, 11)
		end
	end
	
	local copyIDTemp = Copy.isCopyBySceneID(human.scene_id)
	if copyIDTemp == BEGIN_STORY_COPY_NEW_HULAOGUAN then
		local mm = Msg.gc.GC_SCRIPT_START
		mm.script_id = "t1998-1"
		mm.delay = 0
		Msg.send(mm, human.fd)
	end
	
	if copyIDTemp == BEGIN_STORY_COPY_GUANDU then
		local mm = Msg.gc.GC_SCRIPT_START
		mm.script_id = "t1997-1"
		mm.delay = 0
		Msg.send(mm, human.fd)
	end
	
	if copyIDTemp == BEGIN_STORY_COPY_NEW_LUOYANG then
		if human.db.sex == 1 then
			Buffer.add(BEGIN_STORY_LUOYANG_MAN_BUFF_1_ID, human, human)
			Buffer.add(BEGIN_STORY_LUOYANG_MAN_BUFF_2_ID, human, human)
		else
			Buffer.add(BEGIN_STORY_LUOYANG_WOMAN_BUFF_1_ID, human, human)
			Buffer.add(BEGIN_STORY_LUOYANG_WOMAN_BUFF_2_ID, human, human)
		end
	end
end

function onTaskAccept(human, taskID)
  	if human.db.shenbingLen == 0 then
		if taskID == CHOOSE_WEAPON_TASK_ID then
			ShenbingLogic.notifyChooseShenbing(human)
	    end
	end
end

function onTaskFinish(human, taskID)
    if Copy.isCopyBySceneID(human.scene_id) == BEGIN_STORY_COPY_NEW_BORN then
  		if taskID == BEGIN_STORY_BORN_ID then
  			SceneEvent.doSceneEvent(human.scene_id, 2)
  			SceneEvent.doSceneEvent(human.scene_id, 3)   
  			SceneEvent.doSceneEvent(human.scene_id, 101)
  		elseif taskID == BEGIN_STORY_BORN_2_ID then
  	  		SceneEvent.doSceneEvent(human.scene_id, 4)
  			SceneEvent.doSceneEvent(human.scene_id, 5)
  			luolei(human, 1)
  		elseif taskID == BEGIN_STORY_BORN_3_ID then
			Obj.jump(human, 1356,2598, Msg.gc.GC_JUMP)
  		
  	  		SceneEvent.doSceneEvent(human.scene_id, 6)
  	  		SceneEvent.doSceneEvent(human.scene_id, 11)
			removeFriendMonster(human, BEGIN_STORY_MONSTER_ZHAOYUN_ID)
  		elseif taskID == BEGIN_STORY_BORN_4_ID then
  	  		SceneEvent.doSceneEvent(human.scene_id, 7)
  			SceneEvent.doSceneEvent(human.scene_id, 8)
  			SceneEvent.doSceneEvent(human.scene_id, 9)
  			changeAwake(human)
  		elseif taskID == BEGIN_STORY_BORN_5_ID then
  	  		SceneEvent.doSceneEvent(human.scene_id, 10)
  		elseif taskID == BEGIN_STORY_BORN_6_ID then
  			luolei(human, 2)
  		elseif taskID == BEGIN_STORY_BORN_7_ID then
  	  		unchangeAwake(human)
  	  		SceneEvent.doSceneEvent(human.scene_id, 102)
  		end
	end
  
	if taskID == OPEN_HORSE_TASK_ID then
		Buffer.removeID(BEGIN_STORY_BORN_SPEED_DOWN_BUFF_ID, human, true)	
	elseif taskID == BEGIN_STORY_YUZHOU_ENTER_ID then
		CopyHandler.copyEnter(human, BEGIN_STORY_COPY_NEW_YUZHOU)
	elseif taskID == BEGIN_STORY_HULAOGUAN_450_ID then
		CopyHandler.copyEnter(human, BEGIN_STORY_COPY_NEW_HULAOGUAN)
	elseif taskID == BEGIN_STORY_HULAOGUAN_460_ID then
		ObjHuman.enterMap(human, 2003, 6185, 5159)
	elseif taskID == BEGIN_STORY_LUOYANG_550_ID then
		CopyHandler.copyEnter(human, BEGIN_STORY_COPY_NEW_LUOYANG)
	elseif taskID == BEGIN_STORY_GUANDU_690_ID then
		CopyHandler.copyEnter(human, BEGIN_STORY_COPY_GUANDU)
	elseif taskID == BEGIN_STORY_HULAOGUAN_280_ID then
		ObjHuman.enterMap(human, 2001, 642, 4403)
	end
	
	if Copy.isCopyBySceneID(human.scene_id) == BEGIN_STORY_COPY_NEW_YUZHOU then
  		if taskID == BEGIN_STORY_YUZHOU_COPY_170_ID then
  			SceneEvent.doSceneEvent(human.scene_id, 3)
  			SceneEvent.doSceneEvent(human.scene_id, 4)   
  			SceneEvent.doSceneEvent(human.scene_id, 5)
  			
  			SceneEvent.doSceneEvent(human.scene_id, 100) 			
  			
  			--addFriendMonster(human, BEGIN_STORY_YUZHOU_MONSTER_ZHANGFEI_ID)
  		elseif taskID == BEGIN_STORY_YUZHOU_COPY_180_ID then
  			--SceneEvent.doSceneEvent(human.scene_id, 6)  			
  			--removeFriendMonster(human, BEGIN_STORY_YUZHOU_MONSTER_ZHANGFEI_ID)
  			SceneEvent.doSceneEvent(human.scene_id, 110) 			
  			
  		elseif taskID == BEGIN_STORY_YUZHOU_COPY_190_ID then
  		elseif taskID == BEGIN_STORY_YUZHOU_COPY_195_ID then
  			SceneEvent.doSceneEvent(human.scene_id, 7) 
  			SceneEvent.doSceneEvent(human.scene_id, 8) 
  			
  			SceneEvent.doSceneEvent(human.scene_id, 101) 			
  			
  			
  			--addFriendMonster(human, BEGIN_STORY_YUZHOU_MONSTER_ZHANGFEI_ID)
  		elseif taskID == BEGIN_STORY_YUZHOU_COPY_200_ID then
  			SceneEvent.doSceneEvent(human.scene_id, 9) 
  		elseif taskID == BEGIN_STORY_YUZHOU_COPY_210_ID then
  			SceneEvent.doSceneEvent(human.scene_id, 10)	
  		elseif taskID == BEGIN_STORY_YUZHOU_COPY_220_ID then
  			--SceneEvent.doSceneEvent(human.scene_id, 11)	
  			--removeFriendMonster(human, BEGIN_STORY_YUZHOU_MONSTER_ZHANGFEI_ID)
  			
  			SceneEvent.doSceneEvent(human.scene_id, 120) 			
  			
  		elseif taskID == BEGIN_STORY_YUZHOU_COPY_230_ID then
  			--SceneEvent.doSceneEvent(human.scene_id, 12)
  			ObjHuman.enterMap(human, 2002,221,5304)
  		end
	end
end

function isNoExitCopy(copyID)
	for i = 1, #NO_EXIT_COPY_TABLE do
		if copyID == NO_EXIT_COPY_TABLE[i] then return true end
	end
end

function hasFollowNpcCopy(copyID)
	for i = 1, #HAS_FOLLOW_NPC_COPY_TABLE do
		if copyID == HAS_FOLLOW_NPC_COPY_TABLE[i] then return true end
	end
end